package com.space.mygql;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.annotation.NacosProperties;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author <a href="mailto:414563035@qq.com">Bedroom</a>
 * @date 2021/5/27 6:38 下午
 */
@SpringBootApplication
@RestController
public class MygqlApplication implements CommandLineRunner, ApplicationListener<WebServerInitializedEvent> {
    @NacosInjected(properties = @NacosProperties(namespace = "MYGQL"))
    private NamingService namingService;

    private int serverPort;

    @Value("${spring.application.name}")
    String applicationName;

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(MygqlApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        namingService.registerInstance(applicationName, "192.168.3.209", this.serverPort);
        List<Instance> instances = namingService.getAllInstances("mygql-stitching");
        String url = "http://localhost:8080/mygql/v1/schema";
        for (Instance instance : instances) {
            url = url.replace("localhost", instance.getIp()).replace("8080", String.valueOf(instance.getPort()));
            System.out.println(url);
        }
        final String fileName = "schema.graphqls";
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType(MediaType.MULTIPART_FORM_DATA_VALUE);
        headers.setContentType(type);

        //设置请求体，注意是LinkedMultiValueMap
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("files", new ClassPathResource(fileName));
//        form.add("files", new ClassPathResource(fileName));

        //用HttpEntity封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
        String s = restTemplate.postForObject(url, files, String.class);
        System.out.println(s);
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {
        this.serverPort = webServerInitializedEvent.getWebServer().getPort();
    }

    @GetMapping("/hello")
    public Mono<String> hello() {
        return Mono.just("Hello World");
    }
}
